﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Cobit5.DAL;//Please add references
namespace Cobit5.DAL
{
    /// <summary>
    /// 数据访问类:Membership
    /// </summary>
    public partial class Membership
    {
        public Membership()
        { }
        #region  BasicMethod

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string UserId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Membership");
            strSql.Append(" where UserId=@UserId ");
            SqlParameter[] parameters = {
					new SqlParameter("@UserId", SqlDbType.NVarChar,64)			};
            parameters[0].Value = UserId;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Cobit5.Model.Membership model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Membership(");
            strSql.Append("UserId,UserName,Password,PasswordSalt,Email,Mobile,Telephone,PasswordQuestion,PasswordAnswer,IsApproved,IsLockedOut,CreateDate,LastLoginDate,LastPasswordChangedDate,LastLockoutDate,FailedPasswordAttemptCount,FailedPasswordAttemptWindowStart,FailedPasswordAnswerAttemptCount,FailedPasswordAnswerAttemptWindowsStart,Comment)");
            strSql.Append(" values (");
            strSql.Append("@UserId,@UserName,@Password,@PasswordSalt,@Email,@Mobile,@Telephone,@PasswordQuestion,@PasswordAnswer,@IsApproved,@IsLockedOut,@CreateDate,@LastLoginDate,@LastPasswordChangedDate,@LastLockoutDate,@FailedPasswordAttemptCount,@FailedPasswordAttemptWindowStart,@FailedPasswordAnswerAttemptCount,@FailedPasswordAnswerAttemptWindowsStart,@Comment)");
            SqlParameter[] parameters = {
					new SqlParameter("@UserId", SqlDbType.NVarChar,64),
					new SqlParameter("@UserName", SqlDbType.NVarChar,64),
					new SqlParameter("@Password", SqlDbType.NVarChar,128),
					new SqlParameter("@PasswordSalt", SqlDbType.NVarChar,128),
					new SqlParameter("@Email", SqlDbType.NVarChar,256),
					new SqlParameter("@Mobile", SqlDbType.NVarChar,32),
					new SqlParameter("@Telephone", SqlDbType.NVarChar,32),
					new SqlParameter("@PasswordQuestion", SqlDbType.NVarChar,256),
					new SqlParameter("@PasswordAnswer", SqlDbType.NVarChar,128),
					new SqlParameter("@IsApproved", SqlDbType.Bit,1),
					new SqlParameter("@IsLockedOut", SqlDbType.Bit,1),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@LastLoginDate", SqlDbType.DateTime),
					new SqlParameter("@LastPasswordChangedDate", SqlDbType.DateTime),
					new SqlParameter("@LastLockoutDate", SqlDbType.DateTime),
					new SqlParameter("@FailedPasswordAttemptCount", SqlDbType.Int,4),
					new SqlParameter("@FailedPasswordAttemptWindowStart", SqlDbType.DateTime),
					new SqlParameter("@FailedPasswordAnswerAttemptCount", SqlDbType.Int,4),
					new SqlParameter("@FailedPasswordAnswerAttemptWindowsStart", SqlDbType.DateTime),
					new SqlParameter("@Comment", SqlDbType.NVarChar,256)};
            parameters[0].Value = model.UserId;
            parameters[1].Value = model.UserName;
            parameters[2].Value = model.Password;
            parameters[3].Value = model.PasswordSalt;
            parameters[4].Value = model.Email;
            parameters[5].Value = model.Mobile;
            parameters[6].Value = model.Telephone;
            parameters[7].Value = model.PasswordQuestion;
            parameters[8].Value = model.PasswordAnswer;
            parameters[9].Value = model.IsApproved;
            parameters[10].Value = model.IsLockedOut;
            parameters[11].Value = model.CreateDate;
            parameters[12].Value = model.LastLoginDate;
            parameters[13].Value = model.LastPasswordChangedDate;
            parameters[14].Value = model.LastLockoutDate;
            parameters[15].Value = model.FailedPasswordAttemptCount;
            parameters[16].Value = model.FailedPasswordAttemptWindowStart;
            parameters[17].Value = model.FailedPasswordAnswerAttemptCount;
            parameters[18].Value = model.FailedPasswordAnswerAttemptWindowsStart;
            parameters[19].Value = model.Comment;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Cobit5.Model.Membership model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Membership set ");
            strSql.Append("UserName=@UserName,");
            strSql.Append("Password=@Password,");
            strSql.Append("PasswordSalt=@PasswordSalt,");
            strSql.Append("Email=@Email,");
            strSql.Append("Mobile=@Mobile,");
            strSql.Append("Telephone=@Telephone,");
            strSql.Append("PasswordQuestion=@PasswordQuestion,");
            strSql.Append("PasswordAnswer=@PasswordAnswer,");
            strSql.Append("IsApproved=@IsApproved,");
            strSql.Append("IsLockedOut=@IsLockedOut,");
            strSql.Append("CreateDate=@CreateDate,");
            strSql.Append("LastLoginDate=@LastLoginDate,");
            strSql.Append("LastPasswordChangedDate=@LastPasswordChangedDate,");
            strSql.Append("LastLockoutDate=@LastLockoutDate,");
            strSql.Append("FailedPasswordAttemptCount=@FailedPasswordAttemptCount,");
            strSql.Append("FailedPasswordAttemptWindowStart=@FailedPasswordAttemptWindowStart,");
            strSql.Append("FailedPasswordAnswerAttemptCount=@FailedPasswordAnswerAttemptCount,");
            strSql.Append("FailedPasswordAnswerAttemptWindowsStart=@FailedPasswordAnswerAttemptWindowsStart,");
            strSql.Append("Comment=@Comment");
            strSql.Append(" where UserId=@UserId ");
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.NVarChar,64),
					new SqlParameter("@Password", SqlDbType.NVarChar,128),
					new SqlParameter("@PasswordSalt", SqlDbType.NVarChar,128),
					new SqlParameter("@Email", SqlDbType.NVarChar,256),
					new SqlParameter("@Mobile", SqlDbType.NVarChar,32),
					new SqlParameter("@Telephone", SqlDbType.NVarChar,32),
					new SqlParameter("@PasswordQuestion", SqlDbType.NVarChar,256),
					new SqlParameter("@PasswordAnswer", SqlDbType.NVarChar,128),
					new SqlParameter("@IsApproved", SqlDbType.Bit,1),
					new SqlParameter("@IsLockedOut", SqlDbType.Bit,1),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@LastLoginDate", SqlDbType.DateTime),
					new SqlParameter("@LastPasswordChangedDate", SqlDbType.DateTime),
					new SqlParameter("@LastLockoutDate", SqlDbType.DateTime),
					new SqlParameter("@FailedPasswordAttemptCount", SqlDbType.Int,4),
					new SqlParameter("@FailedPasswordAttemptWindowStart", SqlDbType.DateTime),
					new SqlParameter("@FailedPasswordAnswerAttemptCount", SqlDbType.Int,4),
					new SqlParameter("@FailedPasswordAnswerAttemptWindowsStart", SqlDbType.DateTime),
					new SqlParameter("@Comment", SqlDbType.NVarChar,256),
					new SqlParameter("@UserId", SqlDbType.NVarChar,64)};
            parameters[0].Value = model.UserName;
            parameters[1].Value = model.Password;
            parameters[2].Value = model.PasswordSalt;
            parameters[3].Value = model.Email;
            parameters[4].Value = model.Mobile;
            parameters[5].Value = model.Telephone;
            parameters[6].Value = model.PasswordQuestion;
            parameters[7].Value = model.PasswordAnswer;
            parameters[8].Value = model.IsApproved;
            parameters[9].Value = model.IsLockedOut;
            parameters[10].Value = model.CreateDate;
            parameters[11].Value = model.LastLoginDate;
            parameters[12].Value = model.LastPasswordChangedDate;
            parameters[13].Value = model.LastLockoutDate;
            parameters[14].Value = model.FailedPasswordAttemptCount;
            parameters[15].Value = model.FailedPasswordAttemptWindowStart;
            parameters[16].Value = model.FailedPasswordAnswerAttemptCount;
            parameters[17].Value = model.FailedPasswordAnswerAttemptWindowsStart;
            parameters[18].Value = model.Comment;
            parameters[19].Value = model.UserId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string UserId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Membership ");
            strSql.Append(" where UserId=@UserId ");
            SqlParameter[] parameters = {
					new SqlParameter("@UserId", SqlDbType.NVarChar,64)			};
            parameters[0].Value = UserId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string UserIdlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Membership ");
            strSql.Append(" where UserId in (" + UserIdlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Cobit5.Model.Membership GetModel(string UserId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 UserId,UserName,Password,PasswordSalt,Email,Mobile,Telephone,PasswordQuestion,PasswordAnswer,IsApproved,IsLockedOut,CreateDate,LastLoginDate,LastPasswordChangedDate,LastLockoutDate,FailedPasswordAttemptCount,FailedPasswordAttemptWindowStart,FailedPasswordAnswerAttemptCount,FailedPasswordAnswerAttemptWindowsStart,Comment from Membership ");
            strSql.Append(" where UserId=@UserId ");
            SqlParameter[] parameters = {
					new SqlParameter("@UserId", SqlDbType.NVarChar,64)			};
            parameters[0].Value = UserId;

            Cobit5.Model.Membership model = new Cobit5.Model.Membership();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Cobit5.Model.Membership DataRowToModel(DataRow row)
        {
            Cobit5.Model.Membership model = new Cobit5.Model.Membership();
            if (row != null)
            {
                if (row["UserId"] != null)
                {
                    model.UserId = row["UserId"].ToString();
                }
                if (row["UserName"] != null)
                {
                    model.UserName = row["UserName"].ToString();
                }
                if (row["Password"] != null)
                {
                    model.Password = row["Password"].ToString();
                }
                if (row["PasswordSalt"] != null)
                {
                    model.PasswordSalt = row["PasswordSalt"].ToString();
                }
                if (row["Email"] != null)
                {
                    model.Email = row["Email"].ToString();
                }
                if (row["Mobile"] != null)
                {
                    model.Mobile = row["Mobile"].ToString();
                }
                if (row["Telephone"] != null)
                {
                    model.Telephone = row["Telephone"].ToString();
                }
                if (row["PasswordQuestion"] != null)
                {
                    model.PasswordQuestion = row["PasswordQuestion"].ToString();
                }
                if (row["PasswordAnswer"] != null)
                {
                    model.PasswordAnswer = row["PasswordAnswer"].ToString();
                }
                if (row["IsApproved"] != null && row["IsApproved"].ToString() != "")
                {
                    if ((row["IsApproved"].ToString() == "1") || (row["IsApproved"].ToString().ToLower() == "true"))
                    {
                        model.IsApproved = true;
                    }
                    else
                    {
                        model.IsApproved = false;
                    }
                }
                if (row["IsLockedOut"] != null && row["IsLockedOut"].ToString() != "")
                {
                    if ((row["IsLockedOut"].ToString() == "1") || (row["IsLockedOut"].ToString().ToLower() == "true"))
                    {
                        model.IsLockedOut = true;
                    }
                    else
                    {
                        model.IsLockedOut = false;
                    }
                }
                if (row["CreateDate"] != null && row["CreateDate"].ToString() != "")
                {
                    model.CreateDate = DateTime.Parse(row["CreateDate"].ToString());
                }
                if (row["LastLoginDate"] != null && row["LastLoginDate"].ToString() != "")
                {
                    model.LastLoginDate = DateTime.Parse(row["LastLoginDate"].ToString());
                }
                if (row["LastPasswordChangedDate"] != null && row["LastPasswordChangedDate"].ToString() != "")
                {
                    model.LastPasswordChangedDate = DateTime.Parse(row["LastPasswordChangedDate"].ToString());
                }
                if (row["LastLockoutDate"] != null && row["LastLockoutDate"].ToString() != "")
                {
                    model.LastLockoutDate = DateTime.Parse(row["LastLockoutDate"].ToString());
                }
                if (row["FailedPasswordAttemptCount"] != null && row["FailedPasswordAttemptCount"].ToString() != "")
                {
                    model.FailedPasswordAttemptCount = int.Parse(row["FailedPasswordAttemptCount"].ToString());
                }
                if (row["FailedPasswordAttemptWindowStart"] != null && row["FailedPasswordAttemptWindowStart"].ToString() != "")
                {
                    model.FailedPasswordAttemptWindowStart = DateTime.Parse(row["FailedPasswordAttemptWindowStart"].ToString());
                }
                if (row["FailedPasswordAnswerAttemptCount"] != null && row["FailedPasswordAnswerAttemptCount"].ToString() != "")
                {
                    model.FailedPasswordAnswerAttemptCount = int.Parse(row["FailedPasswordAnswerAttemptCount"].ToString());
                }
                if (row["FailedPasswordAnswerAttemptWindowsStart"] != null && row["FailedPasswordAnswerAttemptWindowsStart"].ToString() != "")
                {
                    model.FailedPasswordAnswerAttemptWindowsStart = DateTime.Parse(row["FailedPasswordAnswerAttemptWindowsStart"].ToString());
                }
                if (row["Comment"] != null)
                {
                    model.Comment = row["Comment"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select UserId,UserName,Password,PasswordSalt,Email,Mobile,Telephone,PasswordQuestion,PasswordAnswer,IsApproved,IsLockedOut,CreateDate,LastLoginDate,LastPasswordChangedDate,LastLockoutDate,FailedPasswordAttemptCount,FailedPasswordAttemptWindowStart,FailedPasswordAnswerAttemptCount,FailedPasswordAnswerAttemptWindowsStart,Comment ");
            strSql.Append(" FROM Membership ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" UserId,UserName,Password,PasswordSalt,Email,Mobile,Telephone,PasswordQuestion,PasswordAnswer,IsApproved,IsLockedOut,CreateDate,LastLoginDate,LastPasswordChangedDate,LastLockoutDate,FailedPasswordAttemptCount,FailedPasswordAttemptWindowStart,FailedPasswordAnswerAttemptCount,FailedPasswordAnswerAttemptWindowsStart,Comment ");
            strSql.Append(" FROM Membership ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Membership ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.UserId desc");
            }
            strSql.Append(")AS Row, T.*  from Membership T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "Membership";
            parameters[1].Value = "UserId";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

